package com.newlink.building.login.widget

import android.app.Activity
import android.content.Intent
import android.text.style.ClickableSpan
import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.newlink.building.login.R
import com.newlink.building.common_base.activities.NL_WebViewActivityNL
import com.newlink.building.common_base.utils.NLog

class Module_Login_PolicyClickableSpan(var context: Activity?) :
    ClickableSpan(),
    LifecycleObserver {
    private var mPolicyUrl = ""
    override fun onClick(widget: View) {
        jumpToLicense()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy() {
        NLog.e("[PolicyClickableSpan] ::: onDestory")
        context = null
    }

    private fun jumpToLicense() {
        NLog.e("[PolicyClickableSpan] -> License View")
        if (mPolicyUrl.isEmpty()) {
            return
        }
//        val url =
//            if (LocaleUtils.isSystemLocaleZh()) Constant.PRIVACY_POLICY_PUBLIC_URL else Constant.PRIVACY_POLICY_PUBLIC_URL_EN
        val title = context?.getString(R.string.user_register_license_privacy)
        val intent = Intent(context, NL_WebViewActivityNL::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK
            putExtra("link", mPolicyUrl)
            putExtra("title", title)
        }

        context?.startActivity(intent)
    }

    fun setUrl(url: String?) {
        this.mPolicyUrl = url ?: ""
    }
}
